package TelasFornecedor;

import DAO.FornecedorDAO;
import Pojo.Fornecedor;
import java.util.ArrayList;
import java.util.Collections;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

public class TelaListarFornecedores extends javax.swing.JPanel {
    FornecedorDAO fornecedorDao;
    public TelaListarFornecedores(FornecedorDAO fornecedorDao) {
        initComponents();
        ArrayList<Fornecedor> lista = fornecedorDao.getListaFornecedores();
        DefaultTableModel modelo = (DefaultTableModel) jTable1.getModel();
        
        if(lista.isEmpty()){
            JOptionPane.showMessageDialog(null,"Nenhum Fornecedor Cadastrado");
        }
        Collections.sort(lista);
        for (Fornecedor fornecedor : lista) {
            String [] linha = new String[]{fornecedor.getNome(),fornecedor.getCodigo(),
                fornecedor.getEndereco(),fornecedor.getTelefone() };
            modelo.addRow(linha);
        }
        jTable1.setModel(modelo);
    }
   

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Nome", "Codigo", "Endereco", "Telefone"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jTable1.setEditingColumn(0);
        jTable1.setEditingRow(0);
        jScrollPane1.setViewportView(jTable1);
        jTable1.getColumnModel().getColumn(0).setHeaderValue("Nome");
        jTable1.getColumnModel().getColumn(1).setHeaderValue("Codigo");
        jTable1.getColumnModel().getColumn(2).setHeaderValue("Endereco");
        jTable1.getColumnModel().getColumn(3).setHeaderValue("Telefone");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 520, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 302, Short.MAX_VALUE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    // End of variables declaration//GEN-END:variables
}